package com.max.mil.fuwu.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.max.mil.common.util.DataPage;
import com.max.mil.common.util.StringUtil;
import com.max.mil.fuwu.model.vo.Full;
import com.max.mil.fuwu.model.yhd.FullMinus;
import com.max.mil.fuwu.openapi.yhd.PromotionApi;
import com.max.mil.fuwu.service.FullService;

public class FullServiceImpl implements FullService
{

	PromotionApi promotionApi = new PromotionApi();
	
	
	@Override
	public String addFull(Full full) throws Exception
	{
		long id = promotionApi.addFullMinus(full.getTitle(), full.getRange(), full.getCondition().split(";")[0], full.getCondition().split(";")[1], 
				full.getContent().replace(",", ";"), null, null, null, null, full.getRepeat(), full.getLimit(), full.getJoinLevel(), full.getEffectTime(), full.getExpireTime());
		
		return String.valueOf(id)+"_"+full.getRange();
	}
	
	
	@Override
	public void delFull(String id) throws Exception
	{
		promotionApi.delFullMinus(id.split("_")[0]);
	}
	
	
	@Override
	public Full getFull(String id) throws Exception
	{
		Full full = null;
		FullMinus fullMinus = promotionApi.getFullMinusDetail(
				StringUtil.convertInt(id.split("_")[0], -1), 
				StringUtil.convertInt(id.split("_")[1], -1));
		
		full = new Full();
		full.setId(fullMinus.getId()+"_"+fullMinus.getFullMinusType());
		full.setTitle(fullMinus.getPromotionTitle());
		full.setRange(fullMinus.getFullMinusType());
		full.setStatus(fullMinus.getStatus());
		full.setRepeat(fullMinus.getRepeat());
		full.setJoinLevel(fullMinus.getJoinLevelType());
		full.setCondition(fullMinus.getConditionContents());
		String content = fullMinus.getProductInfo()!=null?fullMinus.getProductInfo().replaceAll(";", ",").trim():"";
		full.setContent(content.endsWith(",")?content.substring(0, content.length()-1):content);
		full.setEffectTime(fullMinus.getStartDate());
		full.setExpireTime(fullMinus.getEndDate());
		
		return full;
	}
	
	@Override
	public DataPage<Full> getFullList(int status, int pageNo, int pageSize) 
			throws Exception
	{
		List<Full> fullList = null;
		
		DataPage<FullMinus> dp = promotionApi.getFullMinus(status, pageNo, pageSize);
		if(dp != null && dp.getList() != null)
		{
			fullList = new ArrayList<Full>();
			
			for(FullMinus fullMinus:dp.getList())
			{
				Full full = new Full();
				full.setId(fullMinus.getId()+"_"+fullMinus.getFullMinusType());
				full.setTitle(fullMinus.getPromotionTitle());
				full.setRange(fullMinus.getFullMinusType());
				full.setStatus(fullMinus.getStatus());
				full.setRepeat(fullMinus.getRepeat());
				full.setJoinLevel(fullMinus.getJoinLevelType());
				full.setCondition(fullMinus.getConditionContents());
				String content = fullMinus.getProductInfo()!=null?fullMinus.getProductInfo().replaceAll(";", ",").trim():"";
				full.setContent(content.endsWith(",")?content.substring(0, content.length()-1):content);
				full.setEffectTime(fullMinus.getStartDate());
				full.setExpireTime(fullMinus.getEndDate());
				
				fullList.add(full);
			}
		}
		
		return new DataPage(fullList, dp.getPageNo(), dp.getPageSize(), dp.getTotalRecord());
	}


	


	

	
}
